package com.zidian.leader.adapter;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.zidian.leader.R;
import com.zidian.leader.common.util.ColorConstants;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.StudentEvaSummary;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *教学质量adapter
 * Created by GongCheng on 2017/1/12.
 */

public class TeachingQualityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<StudentEvaSummary> list;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_student_eva_bar_chart, parent, false);

        return new ItemViewHolder(view);
    }

    /**
     * 选择二级标签接口
     */
    public interface OnTwoIndexSelectListener{
        void onTwoIndexSelected(String twoIndexName);
    }

    private OnTwoIndexSelectListener onTwoIndexSelectListener;

    public void setOnTwoIndexSelectListener(OnTwoIndexSelectListener onTwoIndexSelectListener) {
        this.onTwoIndexSelectListener = onTwoIndexSelectListener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).studentEvaTitleTv.setText(list.get(position).getIndexName());
            initBarChart(((ItemViewHolder) holder).studentEvaChartBc);
            ((ItemViewHolder) holder).studentEvaChartBc.setData(getBarData(position));
            ((ItemViewHolder) holder).studentEvaChartBc.invalidate();
            ((ItemViewHolder) holder).adapter.setData(getTwoIndexList(list.get(position).getTwoIndexList()));
            ((ItemViewHolder) holder).adapter.setOnItemClickListener(new ChartOptionClickListAdapter.OnItemClickListener() {
                @Override
                public void onClick(int twoIndexPosition) {
                    String twoIndexName = list.get(position).getTwoIndexList().get(twoIndexPosition).getTwoIndexName();
                    if (onTwoIndexSelectListener != null) {
                        onTwoIndexSelectListener.onTwoIndexSelected(twoIndexName);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * 得到二级指标名字list
     * @param twoIndexListBeanList
     * @return
     */
    private List<String> getTwoIndexList(List<StudentEvaSummary.TwoIndexListBean> twoIndexListBeanList) {
        List<String> twoIndexList = new ArrayList<>();
        for (int i = 0; i < twoIndexListBeanList.size(); i++) {
            twoIndexList.add(twoIndexListBeanList.get(i).getTwoIndexName());
        }
        return twoIndexList;
    }

    /**
     * 初始化条形图
     *
     * @param chart    BarChart
     */
    private void initBarChart(BarChart chart) {
        chart.setDescription(null);
        chart.setTouchEnabled(false);
        chart.setNoDataText(String.valueOf(R.string.no_data));
        chart.setNoDataTextColor(Color.DKGRAY);
        //设置valueFormatter
//        IAxisValueFormatter valueFormatter = new MyXAxisValueFormatter(stringList);
        XAxis xAxis = chart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(5);
        xAxis.setTextSize(0f);
        //X轴Value设计
//        xAxis.setValueFormatter(valueFormatter);
        xAxis.setGranularity(1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置X轴数字为“”
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return StringConstants.STRING_EMPTY;
            }

            @Override
            public int getDecimalDigits() {
                return 0;
            }
        });
        //Y轴左侧
        YAxis yl = chart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(false);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        //Y轴右侧
        YAxis yr = chart.getAxisRight();
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f);
        yr.setEnabled(false);
        chart.animateY(1500);
        //取消图表说明设置
        Legend l = chart.getLegend();
        l.setEnabled(false);
    }

    /**
     * 得到BarChart的data
     *
     * @param position 位置
     * @return   BarData
     */
    private BarData getBarData(int position) {
        List<BarEntry> barEntries = new ArrayList<>();
        List<StudentEvaSummary.TwoIndexListBean> twoIndexListBeanList = list.get(position).getTwoIndexList();
        for (int i = 0; i < twoIndexListBeanList.size(); i++) {
            barEntries.add(new BarEntry(i + 1, twoIndexListBeanList.get(i).getTwoIndexScore()));
        }
        BarDataSet dataSet = new BarDataSet(barEntries, "barChart");
        dataSet.setColors(ColorConstants.CHART_COLORS);
        BarData barData = new BarData(dataSet);
        barData.setBarWidth(0.3f);
        barData.setValueTextSize(12);
        return barData;
    }

    /**
     * 设置数据
     * @param list StudentEvaSummary List
     */
    public void setList(List<StudentEvaSummary> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.student_eva_title_tv)
        TextView studentEvaTitleTv;
        @Bind(R.id.student_eva_chart_bc)
        BarChart studentEvaChartBc;
        @Bind(R.id.option_list_rv)
        RecyclerView optionListRv;

        private ChartOptionClickListAdapter adapter;

        ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            adapter = new ChartOptionClickListAdapter();
            optionListRv.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
            optionListRv.setAdapter(adapter);
        }
    }
}
